﻿#include "LoadPowerSEABeam.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKLoadPowerSourcesSEABeam.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsLoadManager.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscNodeList.h"
#include "FITK_Component/FITKAcousticsModel/FITKAbstractSEASubSys.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsSEAModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAbstractSEASubSysWithNodes.h"
#include "FITK_Kernel/FITKCore/FITKDataRepo.h"
#include "FITK_Kernel/FITKCore/FITKAbstractNDataObject.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"

LoadPowerSEABeam::LoadPowerSEABeam(const QString & name)
    :PyFITKLoadBase(name)
{
}

bool LoadPowerSEABeam::setLoadPowerProp(QString & flexureX, QString & flexureY, QString & extensionZ, QString & torsionZZ)
{
    if (!_load)return false;

    Acoustics::FITKLoadPowerSourcesSEABeam *load = dynamic_cast<Acoustics::FITKLoadPowerSourcesSEABeam*>(_load);
    if (!load)return false;

    int flexureXID = getSpectraIDByName(flexureX);
    if (flexureXID >= 0)
        load->setFlexureXSpectraID(flexureXID);

    int flexureYID = getSpectraIDByName(flexureY);
    if (flexureYID >= 0)
        load->setFlexureYSpectraID(flexureYID);

    int extensionZID = getSpectraIDByName(extensionZ);
    if (extensionZID >= 0)
        load->setExtensionZSpectraID(extensionZID);

    int torsionZZID = getSpectraIDByName(torsionZZ);
    if (torsionZZID >= 0)
        load->setTorsionZZSpectraID(torsionZZID);

    return true;
}

bool LoadPowerSEABeam::createLoadPower(const QString & name)
{
    if (name == "")return false;
    Acoustics::FITKAcousticsPhysics* phy = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
    if (phy == nullptr) return false;
    Acoustics::FITKAcousticsLoadManager* propMgr = phy->getFITKAcousticsLoadManager();
    if (propMgr == nullptr) return false;
    if (propMgr->getDataByName(name))return false;

    Acoustics::FITKLoadPowerSourcesSEABeam * load = new Acoustics::FITKLoadPowerSourcesSEABeam();
    load->setDataObjectName(name);
    _load = load;
    propMgr->appendDataObj(_load);
    return true;
}

bool LoadPowerSEABeam::setConnection(const QString & obj)
{
    if (!_load)return false;

    int ObjID = FITKDATAREPO->getTDataID< Core::FITKAbstractNDataObject>(obj, true);
    if (ObjID < 0)return false;

    //this->calculateBeamLocationPoint(ObjID, _connectPoint,);

    _load->setConnectionObjID(ObjID);
    _load->setConnectionLocation(_connectPoint);
    return true;
}

void LoadPowerSEABeam::calculateBeamLocationPoint(int objID, double * point, int elemIndex)
{
    Acoustics::FITKAcousticsDiscModel* discModel = FITKAPP->getGlobalData()->getMeshData<Acoustics::FITKAcousticsDiscModel>();
    if (discModel == nullptr && discModel->getSEAModel() == nullptr)return;
    Acoustics::FITKAcousticsSEASubSysManager* seaMgr = discModel->getSEAModel()->getSEASubSysManager();
    Acoustics::FITKAcousticsDiscNodeList* nodeList = discModel->getNodes();
    if (seaMgr == nullptr || nodeList == nullptr)return;
    Acoustics::FITKAbstractSEASubSysWithNodes* data = dynamic_cast<Acoustics::FITKAbstractSEASubSysWithNodes*>(seaMgr->getDataByID(objID));
    QList<int> nodeIDs = data->getNodeIDs();
    if (nodeIDs.size() < elemIndex + 2 || elemIndex < 0) return;
    Core::FITKNode* point1 = nodeList->getNodeByID(nodeIDs.at(elemIndex));
    Core::FITKNode* point2 = nodeList->getNodeByID(nodeIDs.at(elemIndex + 1));
    if (!point1 || !point2) return;
    point[0] = (point1->x() + point2->x()) / 2;
    point[1] = (point1->y() + point2->y()) / 2;
    point[2] = (point1->z() + point2->z()) / 2;
}

